package com.qupai.controller.admin;

import com.qupai.dto.ActivitiesDTO;
import com.qupai.dto.ActivitiesPageQueryDTO;
import com.qupai.entity.Activities;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.service.ActivitiesService;
import com.qupai.vo.ActivitiesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 活动管理
 */
@RestController
@RequestMapping("/admin/activities")
@Tag(name = "志愿-活动相关接口")
@Slf4j
@RequiredArgsConstructor
public class ActivitiesController {

    private final ActivitiesService activitiesService;
    private final RedisTemplate redisTemplate;

    @PostMapping
    @Operation(summary="新增活动")
    public Result<String> save(@RequestBody ActivitiesDTO activitiesDTO){
        log.info("新增活动:{}",activitiesDTO);
        activitiesService.save(activitiesDTO);

        //清理缓存数据
        String key = "activities_" + activitiesDTO.getCollegesId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 活动分页查询
     */
    @GetMapping("/page")
    @Operation(summary="活动分页查询")
    public Result<PageResult> page(@ParameterObject ActivitiesPageQueryDTO activitiesPageQueryDTO){
        log.info("活动分页查询:{}",activitiesPageQueryDTO);
        PageResult pageResult=activitiesService.pageQuery(activitiesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除活动
     */
    @DeleteMapping
    @Operation(summary="删除活动")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除活动：{}", ids);
        activitiesService.deleteBatch(ids);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("activities_*");
        return Result.success();
    }

    /**
     * 根据id查询活动
     */
    @GetMapping("/{id}")
    @Operation(summary="根据id查询活动")
    public Result<ActivitiesVO>getById (@PathVariable Long id){
        log.info("根据id查询活动:{}",id);
        ActivitiesVO activitiesVO=activitiesService.getById(id);
        return Result.success(activitiesVO);
    }

    /**
     *  修改活动
     */
    @PutMapping
    @Operation(summary="修改活动")
    public Result<String> update(@RequestBody ActivitiesDTO activitiesDTO){
        log.info("修改活动:{}",activitiesDTO);
        activitiesService.update(activitiesDTO);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("activities_*");
        return Result.success();
    }

    /**
     *  启用/禁用活动状态
     */
    @PostMapping("/status/{status}")
    @Operation(summary="启用/禁用活动状态")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("启用/禁用活动状态：{}，{}",status,id);
        //调用service
        activitiesService.startOrStop(status,id);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("activities_*");
        return Result.success();
    }

    /**
     * 根据各分类id查询活动
     */
    @GetMapping("/list")
    @Operation(summary="根据各分类id查询活动")
    public Result<List<ActivitiesVO>> list(Activities activities){
        List<ActivitiesVO> list = activitiesService.list(activities);
        log.info("根据各分类id查询活动：{}",list);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
